package com.hotel.sampleTool;

import java.util.List;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;
import com.hotel.BL.HotelBuilderLogic;

public class HotelSample extends RequestSampleData {

	@Override
	public void CreateSample() {
		CreateSample(1);
	}

	@Override
	public void CreateSample(int iteration) {		
		HotelBuilderLogic.getInstance().CreateHotel("K-Hotel");
	}
	
	public PreparedQuery Search(Filter filter)
	{
		Query q = new Query(EntityName.Hotel).setFilter(filter);
		return datastore.prepare(q);
	}
	
	public  Entity SearchHotel(int id)
	{
		Key key = KeyFactory.createKey(EntityName.Hotel, id);
		try {
			return datastore.get(key);
		} catch (EntityNotFoundException e) {
			return null;
		}
	}
	
	public List<Entity> SearchHotelId(int key)
	{
		Filter filter = new FilterPredicate("hotelId",
				                      FilterOperator.EQUAL,
				                      key);
		PreparedQuery pq = Search(filter);
		return pq.asList(FetchOptions.Builder.withLimit(1));
	}
	
	public List<Entity> SearchHotelName(String name)
	{
		Filter filter = new FilterPredicate("hotelName",
				                      FilterOperator.EQUAL,
				                      name);
		PreparedQuery pq = Search(filter);
		return pq.asList(FetchOptions.Builder.withLimit(1));
	}
}
